var util = require('../../utils/util')
var apiUtils = require('../../utils/my_utils')

Page({
    data: {
        navMenu: [{
            title: '观众',
            type: 2,
            active: true,
        }, {
            title: '嘉宾',
            type: 3,
            active: false
        }, {
            title: '媒体',
            type: 4,
            active: false
        }, {
            title: '工作人员',
            type: 5,
            active: false
        }],
        curSigninType: 2,
        curMeeting: undefined,
        formList: [{
            type: 2,
            data: [{
                label: '姓名',
                name: 'name',
                placeholder: '请填写您的姓名',
            }, {
                label: '性别',
                name: 'sex',
            }, {
                label: '工作单位',
                name: 'company',
                placeholder: '请填写工作单位',
            }, {
                label: '身份证号',
                name: 'id_card',
                placeholder: '请填写身份证号码',
            }, {
                label: '手机号',
                name: 'phone',
                placeholder: '请填写手机号',
            }],
        }, {
            type: 3,
            data: [{
                label: '姓名',
                name: 'name',
                placeholder: '请填写您的姓名',
            }, {
                label: '性别',
                name: 'sex',
            }, {
                label: '身份证号',
                name: 'id_card',
                placeholder: '请填写证件号码',
            }, {
                label: '工作单位',
                name: 'company',
                placeholder: '请填写工作单位',
            }, {
                label: '职位',
                name: 'position',
                placeholder: '请填写职位',
            }, {
                label: '手机号',
                name: 'phone',
                placeholder: '请填写手机号',
            }],
        }, {
            type: 4,
            data: [{
                label: '姓名',
                name: 'name',
                placeholder: '请填写您的姓名',
            }, {
                label: '身份证号',
                name: 'id_card',
                placeholder: '请填写身份证号',

            }, {
                label: '媒体单位',
                name: 'organization',
                placeholder: '请填写媒体单位',
            }, {
                label: '手机号',
                name: 'phone',
                placeholder: '请填写手机号',
            }, {
                label: '邮箱',
                name: 'email',
                placeholder: '请填写邮箱',
            }],
        }, {
            type: 5,
            data: [{
                label: '姓名',
                name: 'name',
                placeholder: '请填写您的姓名',
            }, {
                label: '性别',
                name: 'sex',
            }, {
                label: '身份证号',
                name: 'id_card',
                placeholder: '请填写证件号码',
            }, {
                label: '工作类型',
                name: 'postion',
                placeholder: '请填写工作类型',
            }, {
                label: '手机号',
                name: 'phone',
                placeholder: '请填写手机号',
            }]
        }],
        genderPicker: [{
            title: '男',
            type: 1,
            active: true,
            class: 'gender-item',
            icon: 'icon-male'
        }, {
            title: '女',
            type: 2,
            active: false,
            class: 'gender-item',
            icon: 'icon-female'
        }],
        inputProps: {
            sex: '男'
        }
    },
    // nav tab handle
    tabSigninType({
        target
    }) {
        let signType = target.dataset.signintype
        if (!signType) {
            return
        }
        let curNav = this.data.navMenu
        curNav = curNav.map(n => {
            n.active = false
            if (n.type === signType) {
                n.active = true
            }
            return n
        })

        this.setData({
            navMenu: curNav,
            curSigninType: signType
        })
    },
    // gender select
    genderSelect({
        target
    }) {
        let genderType = target.dataset.gender
        if (!genderType) {
            return
        }

        let curGenderPicker = this.data.genderPicker
        curGenderPicker = curGenderPicker.map(g => {
            g.active = false
            if (genderType === g.type) {
                g.active = true
            }
            return g
        })

        let props = this.data.inputProps
        this.setData({
            genderPicker: curGenderPicker,
            inputProps: Object.assign({}, props, {
                gender: genderType
            })
        })
    },
    // bind input value
    bindInputValue(e) {
        let target = e.target
        let value = e.detail.value
        let inputName = target.dataset.name
        if (!value || !inputName) {
            return
        }
        let prevProps = this.data.inputProps

        let props = {}
        if (inputName === 'gender') {
            props[inputName] = value === 1 ? '男' : '女'
        } else {
            props[inputName] = value
        }

        this.setData({
            inputProps: Object.assign({}, prevProps, props)
        })
    },
    // submit form
    validValue(formLabel) {
        let curProps = this.data.inputProps
        for (let p of formLabel) {
            let value = curProps[p.name]
            if (!value) {
                wx.showToast({
                    title: p.placeholder || '请完善发布信息',
                    icon: 'error',
                    image: "/icon/error.png",
                    duration: 500
                })
                return false
            }
            switch (p.name) {
                case 'phone':
                    if (!util.validPhone(value)) {
                        wx.showToast({
                            title: '手机号码格式有误',
                            icon: 'error',
                            image: "/icon/error.png",
                            duration: 500
                        })
                        return false
                    }
                    break
                case 'name':
                    if (!util.validUserName(value)) {
                        wx.showToast({
                            title: '用户名格式有误',
                            icon: 'error',
                            image: "/icon/error.png",
                            duration: 500
                        })
                        return false
                    }
                    break
                case 'id_card':
                    if (!util.validID(value)) {
                        wx.showToast({
                            title: '身份证号有误',
                            icon: 'error',
                            image: "/icon/error.png",
                            duration: 500
                        })
                        return false
                    }
                    break
                case 'email':
                    if (!util.validEmail(value)) {
                        wx.showToast({
                            title: '邮箱格式有误',
                            icon: 'error',
                            image: "/icon/error.png",
                            duration: 500
                        })
                        return false
                    }
                    break
                default:
                    break
            }
        }
        return true
    },
    // valid error handle
    submitProps() {
        let curType = this.data.curSigninType
        let formItem = this.data.formList.reduce((acc, cur) => {
            if (cur.type === curType) {
                acc = cur.data
            }
            return acc
        }, null)

        if (!curType || !formItem) {
            wx.showToast({
                title: '未知错误，返回重试',
                icon: 'error',
                image: "/icon/error.png",
                duration: 500
            })
            return
        }

        if (this.validValue(formItem)) {
            apiUtils.my_request({
                url: 'https://sichuangyi.demopp.in/api-conference-alpha/apply-for-conference',
                method: "POST",
                data: {
                    meeting: this.data.curMeeting.id,
                    user_type: this.data.curSigninType,
                    data: this.data.inputProps
                },
                success: function (resp) {
                    wx.showToast({
                        title: '报名成功',
                        icon: 'success',
                        image: "/icon/success.png",
                        duration: 500
                    })
                    wx.switchTab({
                        url: '/pages/Mypage/index'
                    })
                },
                fail: function (err) {
                    wx.showToast({
                        title: '报名失败' + err,
                        icon: 'error',
                        image: "/icon/error.png",
                        duration: 500
                    })
                }
            })
        }
    },
    onLoad: function (options) {
        let {
            meeting_name,
            meeting_id
        } = options
        if (!meeting_name || !meeting_id) {
            wx.navigateBack()
            return
        }
        this.setData({
            curMeeting: {
                title: meeting_name,
                id: meeting_id
            }
        })
    }
})
